#!/usr/bin/perl
# make a soft link to /usr/lib/cgi-bin/ to add it to the apache
use strict;
use CGI;
use CGI qw/:standard/;
use CGI::Pretty qw/:standard *-table *-div/;
use FindBin '$Bin';
use lib $Bin;
use DBI;
use Data::Dumper;
use CgiUtil;


sub main {
	my $market = url_param('market');
	my $date = url_param('date');
	my $cat = url_param('cat');
	my $file = url_param('file');
	my $test = url_param('test');
	my $group = url_param('group');
	#print $market,"<br>";

	print myhead("仿真结果查询:" . $market ."/". $date ."/". $cat ."/". $file);
	print h3("仿真结果查询:" . $market ."/". $date ."/". $cat ."/". $file);

	if (!($market eq "usa" or $market eq "hk")) {
		if (length($test)<1) {
			print "<a href=\"/cgi-bin/show.cgi?market=usa\" target=\"_blank\">USA</a><br>" ;
			print "<a href=\"/cgi-bin/show.cgi?market=hk\" target=\"_blank\">HK</a>" ;
		} else {
			print "<a href=\"/cgi-bin/show.cgi?market=usa&test=$test&group=$group\" target=\"_blank\">USA</a><br>" ;
			print "<a href=\"/cgi-bin/show.cgi?market=hk&test=$test&group=$group\" target=\"_blank\">HK</a>" ;
		}
	} else {
		my $res_path = "";
		if ($market eq "usa") {
			$res_path = "/data/share/code/trunk/perl/stock/USA/usa_best_stock/data/rank";
		} else {
			$res_path = "/data/share/code/trunk/perl/stock/USA/hk_best_stock/data/rank";
		}

		if (length($date) < 1) {
			my @date = `ls $res_path`;
			print "Choose a Date:<br>";
			foreach my $key (@date) {
				if (length($test)<1) {
					print "<a href=\"/cgi-bin/show.cgi?market=$market&date=$key\" target=\"_blank\">$key</a><br>" ;
				} else {
					print "<a href=\"/cgi-bin/show.cgi?market=$market&date=$key&test=$test&group=$group\" target=\"_blank\">$key</a><br>" ;
				}
			}
		} else {
			if (length($cat)<1) {
				my @cat = `ls $res_path/$date/`;
				foreach my $key (@cat) {
					if (length($test)<1) {
						print "<a href=\"/cgi-bin/show.cgi?market=$market&date=$date&cat=$key\" target=\"_blank\">$key</a><br>" ;
					} else {
						print "<a href=\"/cgi-bin/show.cgi?market=$market&date=$date&cat=$key&test=$test&group=$group\" target=\"_blank\">$key</a><br>" ;
					}
				}
			} else {
				if (length($file) < 1) {
					my @file = `ls $res_path/$date/$cat/`;
					foreach my $key (@file) {
						if (length($test)<1) {
							print "<a href=\"/cgi-bin/show.cgi?market=$market&date=$date&cat=$cat&file=$key\" target=\"_blank\">$key</a><br>" ;
						} else {
							print "<a href=\"/cgi-bin/show.cgi?market=$market&date=$date&cat=$cat&file=$key&test=$test&group=$group\" target=\"_blank\">$key</a><br>" ;
						}
					}
				} else {
					if (1) {
						my @lines = `cat $res_path/$date/$cat/$file`;
						print "<h4>path=$res_path/$date/$cat/$file</h4><br>";
						foreach my $line (@lines) {
							chomp($line);
							if ($line =~ /(.+)Name:(\w+)(.+)/) {
								my ($idx,$name, $other) = ($1, $2, $3);
								if (length($test)<1 or length($group)<1) {
									print "$idx"."Name:" . "<a href=\"http://xueqiu.com/S/$name\" target=\"_blank\">$name</a>" . $other . "<br>";
								} else {
									my $price = 1;
									if ($line =~ /CurrentValue:(.+?),/) {
										$price = $1;
									}
									my $shares = 10000 / ($price + 0.000000000001);
									my $buy_to_xueqiu = "<a href=\"/cgi-bin/stock_buy.cgi?group=$group&price=$price&date=$date&code=$name&shares=$shares&market=$market\" target=\"_blank\">buy</a>";
									print "$idx"."Name:" . "<a href=\"http://xueqiu.com/S/$name\" target=\"_blank\">$name</a>" . $other . "," . $buy_to_xueqiu . "<br>";
								}
							} else {
								print "$line<br>";
							}
						}
					} 
				}
			}
		}
	}
	print end_html;
}

main();

